/**
 * 格式化日期方法
 * @export parseTime
 * @param time {String|Date} 日期数据
 * @param cFormat {String} 格式化类型
 * @example parseTime(new Date(),'{y}-{m}-{d}');
 * @returns {String} 默认年-月-日
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


export function formatTime(time, option) {
  time = +time * 1000
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

//废弃
// export function getTime(option) {
//   let time = new Date()
//   function clearTimer(val){
//     if(val>=1&&val<=9){
//       val='0'+val;
//     }
//     return val
//   }
//   let year = time.getFullYear();
//   let month =clearTimer(time.getMonth() + 1);
//   let day = clearTimer(time.getDate());
//   let hour = clearTimer(time.getHours());
//   let minutes = clearTimer(time.getMinutes());
//   let seconds = clearTimer(time.getSeconds());
//   if(option=='yyyy-MM-dd'){
//    return year+'-'+month+'-'+day+''
//   }else{
//     return year+'-'+month+'-'+day+' '+hour+':'+minutes+':'+seconds
//   }
// }


export function timeFormat(cjsj) {
  var date = new Date(cjsj) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  var D = (date.getDate()) < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s
}
export function timeFormatGoods(cjsj) {
  var date = new Date(cjsj) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  var D = (date.getDate()) < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  if ((h + m + s) == '00:00:00' && (M + D) == '00-00') {
    return Y
  } else if ((h + m + s) == '00:00:00') {
    return Y + M + D

  } else {
    return Y + M + D + h + m + s
  }
}
export function filterEmptyArray(arr) {
  let newarr = arr.filter((item, index) => {
    let u = Object.values(item)
    if (u.find(n => n)) {
      return item
    }
  })
  return newarr
}
/**
 * 判断空对象
 * @export isEmptyObject
 * @param obj {Object} 对象
 * @example isEmptyObject({});
 * @returns {Boolean}
 */
export function isEmptyObject(obj) {
  for (var n in obj) {
    return false;
  }
  return true;
}
/**
 * 格式化字符串
 * @export formatString
 * @param str {Object} 字符串模板
 * @param data {Object|String|Number} 数据
 * @example formatString("api/values/{id}/{name}",{id:101,name:"test"})
 * formatString("api/values/{0}/{1}",101,"test")
 * @returns {String}
 */
export function formatString(str, data) {
  if (!str || data == undefined) {
    return str;
  }
  if (typeof data === "object") {
    for (let key in data) {
      if (data.hasOwnProperty(key)) {
        str = str.replace(new RegExp("\{" + key + "\}", "g"), data[key]);
      }
    }
  } else {
    let args = arguments,
      reg = new RegExp("\{([0-" + (args.length - 1) + "])\}", "g");
    return str.replace(reg, function (match, index) {
      return args[index - (-1)];
    });
  }
  return str;
}

/**
 * 配置静态资源文件路径
 * @export addURL
 * @param url {String} 字符串模板
 * @param path {String} 数据
 * @example addURL("/static/img/aa.jpg","1")
 * @returns {String}
 */
// let hostName = location.origin.indexOf("localhost") > -1 ? 'https://lxerptest.66123123.com' : location.origin;
export function addURL(url,path) {
  path=path||''
  let hostName = location.origin.indexOf("localhost") > -1 ? 'https://lxerptest'+`${path}`+'.66123123.com' : location.origin;
  let str;
  str = hostName + '/image' + url
  return str;
}

export function returnParseInt(value) { //返回正浮点数
  if (Number.isFinite(parseFloat(value))) {
    var val = Math.round(parseFloat(value) * 100) / 100;
    var xsd = val.toString().split(".");
    if (xsd.length == 1) {
      val = val.toString() + ".00";
      return val;
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        val = val.toString() + "0";
      }
      return val;
    }
  } else {
    return 0
  }
}
/**
 * 对数字保留两位小数不足2位自动补零-支持负数
 * @export rFloat
 * @param value {Number} 数值
 * @example rFloat(-25.624)
 * @returns {Number}
 */
export function rFloat(value) {
  if (parseFloat(value)) {
    var val = Math.round(parseFloat(value) * 100) / 100;
    var xsd = val.toString().split(".");
    if (xsd.length == 1) {
      val = val.toString() + ".00";
      return val;
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        val = val.toString() + "0";
      }
      return val;
    }
  } else {
    return 0
  }
}
//财务模块，不需要保留两位小数补0
export function rrFloat(val) {
  if (parseFloat(val)) {
    var xsd = val.toString().split(".");
    if (xsd.length == 1) {
      val = val.toString() + ".0000";
      return val;
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        val = val.toString() + "000";
      }
      if (xsd[1].length == 2) {
        val = val.toString() + "00";
      }
      return val;
    }
  } else {
    return '0.0000'
  }
}
/**
 * 对数字保留两位小数不足2位自动补零
 * @export returnFloat
 * @param value {Number} 数值
 * @example returnFloat(25.624)
 * @returns {Number}
 */
export function returnFloat(value) {
  if (parseFloat(value) >= 0) {
    var val = Math.round(parseFloat(value) * 100) / 100;
    var xsd = val.toString().split(".");
    if (xsd.length == 1) {
      val = val.toString() + ".00";
      return val;
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        val = val.toString() + "0";
      }
      return val;
    }
  } else {
    return '0.00'
  }

}
// returnFloatTwoFour没有使用，只有14个引用
export function returnFloatTwoFour(value) {
  if (parseFloat(value) >= 0) {
    var val = Math.round(parseFloat(value) * 100) / 100;
    var xsd = val.toString().split(".");
    if (xsd.length == 1) {
      val = val.toString() + ".0000";
      return val;
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        val = val.toString() + "000";
      }
      if (xsd[1].length == 2) {
        val = val.toString() + "00";
      }
      return val;
    }
  } else {
    return '0.0000'
  }

}
export function returnFloatFour(value) {
  if (parseFloat(value)) {
    var val = Math.round(parseFloat(value) * 10000) / 10000;
    var xsd = val.toString().split(".");
    if (xsd.length == 1) {
      val = val.toString() + ".0000";
      return val;
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        val = val.toString() + "000";
      }
      if (xsd[1].length == 2) {
        val = val.toString() + "00";
      }
      if (xsd[1].length == 3) {
        val = val.toString() + "0";
      }
      return val;
    }
  } else {
    return '0.0000'
  }
}
/**
 * 防抖功能函数
 * @export Debounce
 * @param fn {Function} 调用的函数
 * @param delay {Number} 延迟时间默认300ms
 * @example Debounce(fun,500)
 * @returns {Function}
 */
export const Debounce = (fn, delay = 300) => {
  let timer, timeStamp = 0;
  let context, args;
  let run = () => {
    timer = setTimeout(() => {
      fn.apply(context, args);
    }, delay);
  }
  let clean = () => {
    clearTimeout(timer);
  }

  return function () {
    context = this;
    args = arguments;
    let now = (new Date()).getTime();

    if (now - timeStamp < delay) {
      clean();
      run();
    } else {
      run();
    }
    timeStamp = now;
  }
};

export function downloadFile(data) {
  if (!data) {
    return
  }
  let url = window.URL.createObjectURL(new Blob([data], {
    type: "application/xls"
  }))
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', '导出文件.xls')
  document.body.appendChild(link)
  link.click()
}

export function download(data, fileName) {
  if (!data) {
    return
  }
  let url = window.URL.createObjectURL(new Blob([data], {
    type: "application/xls"
  }))
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', `${fileName}.xls`)
  document.body.appendChild(link)
  link.click()
}

export function print() {
  var newstr = document.getElementById('print').innerHTML
  document.body.innerHTML = newstr
  var oldstr = document.body.innerHTML
  window.print()
  window.location.reload()
  document.body.innerHTML = oldstr
  return false
}

/**
 * 倒计时功能函数
 * @export countdownTime
 * @param endTime {String} 结束时间
 * @param sysDateTime {String} 系统当前时间
 * @param callback {Function} 回调函数
 * @example let cd = new countdownTime()
 * cd.startUp(et, syst, cb)
 * cd.shutDown()
 */
export function countdownTime() {
  this.timeout = null;       
}
countdownTime.prototype.startUp = function (endTime, sysDateTime, callback) {
  var date = new Date(sysDateTime.replace(/-/g, '/')).getTime();
  var until = new Date(endTime.replace(/-/g, '/'));
  this.timeout = setInterval(function () {
    // 获取现在的时间
    date += 1000;
    // 计算时会发生隐式转换，调用valueOf()方法，转化成时间戳的形式
    var days = (until - date) / 1000 / 3600 / 24;
    // 下面都是简单的数学计算
    var day = Math.floor(days);
    var hours = (days - day) * 24;
    var hour = Math.floor(hours);
    var minutes = (hours - hour) * 60;
    var minute = Math.floor(minutes);
    var seconds = (minutes - minute) * 60;
    var second = Math.floor(seconds);
    var back = day + '天' + hour + '小时' + minute + '分钟' + second + '秒';
    callback(back);
  }, 1000)
}

countdownTime.prototype.shutDown = function () {
  clearInterval(this.timeout);
  this.timeout = null;
}
/**
 * 倒计时功能函数
 * @export getBrowserType
 * @example getBrowserType()
 * @returns {String}
 */
export function getBrowserType() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
  var isIE = userAgent.indexOf("compatible") > -1 &&
    userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
  var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
  var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
  var isSafari = userAgent.indexOf("Safari") > -1 &&
    userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
  var isChrome = userAgent.indexOf("Chrome") > -1 &&
    userAgent.indexOf("Safari") > -1; //判断Chrome浏览器

  if (isIE) {
    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
    if (fIEVersion == 7) {
      return "IE7";
    } else if (fIEVersion == 8) {
      return "IE8";
    } else if (fIEVersion == 9) {
      return "IE9";
    } else if (fIEVersion == 10) {
      return "IE10";
    } else if (fIEVersion == 11) {
      return "IE11";
    } else {
      return "0";
    } //IE版本过低
    return "IE";
  }
  if (isOpera) {
    return "Opera";
  }
  if (isEdge) {
    return "Edge";
  }
  if (isFF) {
    return "Firfox";
  }
  if (isSafari) {
    return "Safari";
  }
  if (isChrome) {
    return "Chrome";
  }
}

export function formDate(interval,timeValue) {
  let time =Date.parse(timeValue)||Date.parse(new Date());
  return new Date(time-interval*24*60*60*1000)

  // let day = time.getDate()
  // let month = time.getMonth()
  // time.setDate(day - 30)
  // if (day <= 30) {
  //   time.setMonth(month)
  // } 
  // // console.log(month, day, parseTime(time), 77777)
  // return time
}

export function matchDate(time1,time2){
  if(time1&&time2){
    return (Date.parse(time2)-Date.parse(time1))/(24*60*60*1000)
  }else{
    return null
  }
  
}

export function round(num,b=2){
   let n= Math.pow(10,b);
   return Math.round(n*num)/n                    
}